import acs.bridge.Bridge;
import acs.bridge.BridgeInterface;
import acs.bridge.BridgeList;
import org.apache.log4j.Logger;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Iterator;

/**
 * Created with IntelliJ IDEA.
 * User: stas
 * Date: 16.03.13
 * Time: 23:18
 * To change this template use File | Settings | File Templates.
 */
public class Main {
    Logger logger = Logger.getLogger(this.getClass().getName());


    public static String getStackTrace(Throwable throwable) {
        Writer writer = new StringWriter();
        PrintWriter printWriter = new PrintWriter(writer);
        throwable.printStackTrace(printWriter);
        return writer.toString();
    }


    public String getVersion() {
     /*   if (version != null) {
            return version;
        } */

        // try to load from maven properties first
     /*   try {
            Properties p = new Properties();
            InputStream is = getClass().getResourceAsStream("/META-INF/maven/com.my.group/my-artefact/pom.properties");
            if (is != null) {
                p.load(is);
                version = p.getProperty("version", "");
            }
        } catch (Exception e) {
            // ignore
        }

        // fallback to using Java API
        if (version == null) {
            Package aPackage = getClass().getPackage();
            if (aPackage != null) {
                version = aPackage.getImplementationVersion();
                if (version == null) {
                    version = aPackage.getSpecificationVersion();
                }
            }
        }

        if (version == null) {
            // we could not compute the version so use a blank
            version = "";
        }
                 */
        return "1";
    }


    public static void main(String[] args) {

        Main main = new Main();

       // main.logger.info("Bridge ver " + main.getVersion());
        try {
       /* Чтение файла настроек */
            if (args.length < 1) {

                main.logger.info("Usage: spring-config.xml");
                System.exit(0);
            }

            System.getProperties().put("jasypt_psw", "CVBVh2cVlev8hdjWK5s9rS/Ntz6xH41d");

            //ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("config/spring-config.xml");
            FileSystemXmlApplicationContext context = new FileSystemXmlApplicationContext(new String[]
                    {args[0]});
            BridgeInterface bridge = null;
            try {
                BridgeList bridgeList = (BridgeList) context.getBean("BridgeList");
                Iterator iterator = bridgeList.getBridgeList().iterator();

                while (iterator.hasNext()) {
                    bridge = (BridgeInterface) iterator.next();
                    bridge.executeBridge();
                }
            } catch (Exception e) {
                bridge = (BridgeInterface) context.getBean("Bridge");
                bridge.executeBridge();
            }
        } catch (Exception e) {
            main.logger.error(getStackTrace(e));
        }
    }

}
